package com.breaktian.demoapp.ui.jetpack;


import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.breaktian.demoapp.R;
import com.breaktian.demoapp.ui.jetpack.viewmodel.TestViewModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class JetPackFragment extends Fragment {


    private View mRootView;
    private TextView textView;

    public JetPackFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mRootView = inflater.inflate(R.layout.fragment_jet_pack, container, false);
        initView();
        return mRootView;
    }

    private void initView() {
        textView = (TextView)mRootView.findViewById(R.id.textView);
        getViewModel().getBean().observe(this, new Observer<TestViewModel.Bean>() {
            @Override
            public void onChanged(@Nullable TestViewModel.Bean bean) {
                textView.setText(bean.title);
            }
        });
    }

    protected TestViewModel getViewModel() {
        ViewModelProvider.AndroidViewModelFactory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication());
        ViewModelProvider provider = new ViewModelProvider(this.getActivity(), factory);
        return provider.get(TestViewModel.class);
    }

}
